package com.fenghongzhang.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.fenghongzhang.day003.db.DaoMaster;
import com.fenghongzhang.day003.db.DaoSession;
import com.fenghongzhang.day003.db.StudentDao;
import com.fenghongzhang.day003.db.UserDao;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private UserDao userDao;
    private static final String TAG = "MainActivity";
    private StudentDao studentDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        userDao = DaoManger.getInstance().getDaoSession().getUserDao();

        //select * from USER where NAME = "?

    }

    public void add(View view) {

//        User user = new User();
////        user.setId();
//        user.setName("xiaoming");
//        user.setPwd("123");
//        userDao.insert(user);

        Student student = new Student();
        student.setName("哈哈");
        studentDao.insert(student);
    }

    public void select(View view) {
        List<User> users = userDao.loadAll();
        Toast.makeText(this, users.size()+"", Toast.LENGTH_SHORT).show();
    }

    public void del(View view) {
        User user = new User();
        user.setId(1l);
        user.setName("xiaoming");
        user.setPwd("123");
        userDao.delete(user);

    }

    public void update(View view) {
        User user = new User();
        user.setId(2l);
        user.setName("daming");
        user.setPwd("345");
        userDao.update(user);

    }

    public void selectById(View view) {
        //唯一
//        User unique = userDao.queryBuilder().where(UserDao.Properties.Id.eq("2")).build().unique();
//        Toast.makeText(this, unique.getName(), Toast.LENGTH_SHORT).show();
        //列表
//        List<User> list = userDao.queryBuilder().where(UserDao.Properties.Id.eq("2")).build().list();
//        User user = list.get(0);

        //分页
        //select * from user limit 1,3
//        int i =0;
//        List<User> list = userDao.queryBuilder().offset(i * 2).limit(2).build().list();
//        Toast.makeText(this, list.size()+"", Toast.LENGTH_SHORT).show();

        //根据id查询
//        User user = userDao.loadByRowId(2L);


    }
}
